package org.ecype.diego;

import java.lang.reflect.Field;

import org.ecype.util.Nulls;

import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import androidx.persistence.access.Subdirectory;

public class SubdirectoryProcessor extends AbstractUriProcessor {

	private String where;
	private String order;

	@Override
	public void setup(ContractContentProvider provider, Field field) {
		super.setup(provider, field);
		Subdirectory annotation = field.getAnnotation(Subdirectory.class);
		where = annotation.where();
		where = !TextUtils.isEmpty(where) ? where : null;
		order = annotation.order();
		order = !TextUtils.isEmpty(order) ? order : null;
	}

	@Override
	public Cursor query(Uri uri, Uri notifyUri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Nulls.checkAreAreNull(projection, selection, selectionArgs, sortOrder);
		Uri dir = Uris.withoutLastSegment(uri);
		return provider.query(dir, notifyUri, projection, where, null, order);
	}

	@Override
	public int delete(Uri uri, Uri notifyUri, String selection, String[] selectionArgs) {
		Nulls.checkAreAreNull(selection, selectionArgs);
		Uri dir = Uris.withoutLastSegment(uri);
		return provider.delete(dir, notifyUri, where, null);
	}

	@Override
	public String getType(Uri uri) {
		Uri dir = Uris.withoutLastSegment(uri);
		return provider.getType(dir);
	}

}
